package com.zhangxin.aiInterview.service.impl;

import com.zhangxin.aiInterview.common.ErrorCode;
import com.zhangxin.aiInterview.common.ResultUtils;
import com.zhangxin.aiInterview.constant.FileConstant;
import com.zhangxin.aiInterview.exception.BusinessException;
import com.zhangxin.aiInterview.manager.cos.CosManager;
import com.zhangxin.aiInterview.manager.work_flow.generate_internship_introduction.GenerateInternshipIntroduction;
import com.zhangxin.aiInterview.manager.work_flow.generate_interview_question.GenerateInterviewQuestion;
import com.zhangxin.aiInterview.manager.work_flow.generate_job_introduction.GenerateJobIntroduction;
import com.zhangxin.aiInterview.manager.work_flow.generate_project_introduction.GenerateProjectIntroduction;
import com.zhangxin.aiInterview.manager.work_flow.generate_self_introduction.GenerateSelfIntroduction;
import com.zhangxin.aiInterview.service.SpecialExerciseService;
import com.zhangxin.aiInterview.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;

@Service
@Slf4j
public class SpecialExerciseServiceImpl implements SpecialExerciseService {
    @Resource
    private UserService userService;
    @Resource
    private CosManager cosManager;
    public static final String RESUME_PREFIX = "ai_interview/resume_pdf";
    /**
     * 上传简历 专项练习
     * @param file
     * @param type
     * @param request
     * @return
     */
    @Override
    public String upload(MultipartFile file, String type, HttpServletRequest request) {
        Long userId = userService.getLoginUser(request).getId();
        // 文件目录：根据业务、用户来划分
        String uuid = RandomStringUtils.randomAlphanumeric(8);
        String filename = uuid + "-" + file.getOriginalFilename();
        String filepath = String.format("/%s/%s/%s", RESUME_PREFIX, userId, filename);
        File tempFile = null;
        final String[] tecInterviewVideoUrl = new String[1];
        try {
            // 1.上传简历文件到COS
            tempFile = File.createTempFile(filepath, null);
            file.transferTo(tempFile);
            cosManager.putObject(filepath, tempFile);
            // 2.拼接访问链接
            tecInterviewVideoUrl[0] = FileConstant.COS_HOST + filepath;
            // 3.调用工作流专项训练
            return specialExerciseByWorkFlow(tecInterviewVideoUrl[0], type);

        } catch (Exception e) {
            log.error("简历上传失败, filepath = " + filepath, e);
            // 删除cos中的文件
            cosManager.deleteObject(filepath);
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, e.getMessage());
        } finally {
            if (tempFile != null) {
                // 删除临时文件
                boolean delete = tempFile.delete();
                if (!delete) {
                    log.error("文件删除失败, filepath = {}", filepath);
                }
            }
            //删除cos中的简历文件
            cosManager.deleteObject(filepath);
        }
    }

    /**
     * 调用工作流专项训练
     * @param resumeUrl
     * @param type
     * @return
     */
    public String specialExerciseByWorkFlow(String resumeUrl, String type) {
        if("self_introduction".equals(type)){
            return GenerateSelfIntroduction.generateSelfIntroduction(resumeUrl);
        } else if ("internship_introduction".equals(type)) {
            return GenerateInternshipIntroduction.generateInternshipIntroduction(resumeUrl);
        } else if ("job_introduction".equals(type)) {
            return GenerateJobIntroduction.generateJobIntroduction(resumeUrl);
        } else if ("project_introduction".equals(type)) {
            return GenerateProjectIntroduction.generateProjectIntroduction(resumeUrl);
        }else if ("interview_question".equals(type)){
            //面试押题
            return GenerateInterviewQuestion.generateInterviewQuestion(resumeUrl);
        }
        return "";
    }
}
